package com.quanyue.haircut.shop.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.Card;
import com.quanyue.haircut.domain.entity.CardScope;
import com.quanyue.haircut.domain.entity.User;
import com.quanyue.haircut.shop.service.CardService;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "会员卡片")
@Scope(role = UserRole.USER)
@RestController
@RequestMapping("/api")
public class CardController {
    @Autowired
    private CardService cardService;

    @ApiOperation(value = "查询当前店铺下的会员信息")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/card/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <User>> findUserByShop(@RequestBody PageRequest request,
                                                      @RequestParam Integer shopId) {
        return ServiceResult.success(cardService.findUserByShop(request, shopId));
    }

    @ApiOperation(value = "查询自己的会员信息")
    @GetMapping(value = "/card", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <Card>> findCardByUser(@ApiIgnore Principal principal) {
        return ServiceResult.success(cardService.findCardByUser(principal.getId()));
    }

    @ApiOperation(value = "添加会员")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/card", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Card> createCard(@RequestBody Card card, @ApiIgnore Principal principal) {
        card.setMerchantNo(principal.getMerchantNo());
        return ServiceResult.success(cardService.createCard(card));
    }

    @ApiOperation(value = "更新会员")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/card", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateCard(@RequestBody Card card) {
        cardService.updateCard(card);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除会员")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/card", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteCard(@RequestParam Integer cardId) {
        cardService.deleteCard(cardId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "设置会员卡是否通用")
    @Scope(role = UserRole.MANAGER)
    @GetMapping(value = "/card/scope", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> setCardScope(@RequestParam Boolean isCommon, @ApiIgnore Principal principal) {
        cardService.updateCardScope(isCommon, principal.getMerchantNo());
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "查询会员卡通用状态")
    @Scope(role = UserRole.MANAGER)
    @GetMapping(value = "/card/scope/select", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <CardScope> cardScope(@ApiIgnore Principal principal) {
        return ServiceResult.success(cardService.selectCardScope(principal.getMerchantNo()));
    }
}
